package com.zkt.subject.application.controller;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.subject.application.convert.SubjectUserRecordDTOConverter;
import com.zkt.subject.entity.SubjectUserRecordDTO;
import com.zkt.subject.common.entity.PageResult;
import com.zkt.subject.common.entity.Result;
import com.zkt.subject.common.util.LoginUtil;
import com.zkt.subject.domain.entity.SubjectUserRecordBO;
import com.zkt.subject.domain.service.SubjectUserRecordDomainService;
import com.zkt.subject.infra.entity.UserInfo;
import com.zkt.subject.infra.rpc.UserRPC;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/29
 * @description 用户做题记录controller层
 **/

@RestController
@RequestMapping("/userRecord")
@Slf4j
public class SubjectUserRecordController {
	
	@Resource
	private UserRPC userRPC;
	
	@Resource
	private SubjectUserRecordDomainService subjectUserRecordDomainService;
	
	/**
	 * 增加用户做题记录
	 *
	 * @param subjectUserRecordDTO 用户做题记录
	 * @return 是否成功
	 */
	@PostMapping("add")
	public Result<Boolean> add(@RequestBody SubjectUserRecordDTO subjectUserRecordDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectUserRecordController.add.dto:{}", JSON.toJSONString(subjectUserRecordDTO));
			}
			
			if (ObjUtil.isEmpty(subjectUserRecordDTO.getUserId())) {
				UserInfo userInfo = userRPC.getUserInfo(LoginUtil.getLoginId());
				if (ObjUtil.isEmpty(userInfo) || ObjUtil.isEmpty(userInfo.getId())) {
					throw new Exception("用户id不能为空");
				}
				
				subjectUserRecordDTO.setUserId(userInfo.getId());
			}
			if (ObjUtil.isEmpty(subjectUserRecordDTO.getSubjectId())) {
				throw new Exception("题目id不能为空");
			}
			if (ObjUtil.isEmpty(subjectUserRecordDTO.getSubjectScore())) {
				throw new Exception("题目分数不能为空");
			}
			if (ObjUtil.isEmpty(subjectUserRecordDTO.getUserScore())) {
				throw new Exception("用户获得分数不能为空");
			}
			if (ObjUtil.isEmpty(subjectUserRecordDTO.getUseTime())) {
				throw new Exception("本题耗时不能为空");
			}
			if (ObjUtil.isEmpty(subjectUserRecordDTO.getLabelId())) {
				throw new Exception("标签id不能为空");
			}
			
			SubjectUserRecordBO subjectUserRecordBO = SubjectUserRecordDTOConverter.INSTANCE.convertDtoToBo(subjectUserRecordDTO);
			
			return Result.ok(subjectUserRecordDomainService.add(subjectUserRecordBO));
		} catch (Exception e) {
			log.error("SubjectUserRecordController.add.error:{}", e.getMessage(), e);
			return Result.fail("添加用户做题记录失败");
		}
	}
	
	/**
	 * 获取用户做题记录（分页）
	 *
	 * @param subjectUserRecordDTO 查询参数
	 * @return 查询结果
	 */
	@GetMapping("getSubjectUserRecord")
	public Result<PageResult<SubjectUserRecordDTO>> getSubjectUserRecord(SubjectUserRecordDTO subjectUserRecordDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectUserRecordController.getSubjectUserRecord.dto:{}", JSON.toJSONString(subjectUserRecordDTO));
			}
			
			if (ObjUtil.isEmpty(subjectUserRecordDTO.getUserId())) {
				UserInfo userInfo = userRPC.getUserInfo(LoginUtil.getLoginId());
				if (ObjUtil.isEmpty(userInfo) || ObjUtil.isEmpty(userInfo.getId())) {
					throw new Exception("用户id不能为空");
				}
				
				subjectUserRecordDTO.setUserId(userInfo.getId());
			}
			
			SubjectUserRecordBO subjectUserRecordBO = SubjectUserRecordDTOConverter.INSTANCE.convertDtoToBo(subjectUserRecordDTO);
			
			return Result.ok(subjectUserRecordDomainService.getSubjectUserRecord(subjectUserRecordBO));
		} catch (Exception e) {
			log.error("SubjectUserRecordController.getSubjectUserRecord.error:{}", e.getMessage(), e);
			return Result.fail("获取用户做题记录失败");
		}
	}
	
	/**
	 * 获取用户所有的做题记录
	 *
	 * @param subjectUserRecordDTO 查询参数
	 * @return 查询结果
	 */
	@GetMapping("getAllSubjectUserRecord")
	public Result<List<SubjectUserRecordDTO>> getAllSubjectUserRecord(SubjectUserRecordDTO subjectUserRecordDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectUserRecordController.getAllSubjectUserRecord.dto:{}", JSON.toJSONString(subjectUserRecordDTO));
			}
			
			if (ObjUtil.isEmpty(subjectUserRecordDTO.getUserId())) {
				UserInfo userInfo = userRPC.getUserInfo(LoginUtil.getLoginId());
				if (ObjUtil.isEmpty(userInfo) || ObjUtil.isEmpty(userInfo.getId())) {
					throw new Exception("用户id不能为空");
				}
				
				subjectUserRecordDTO.setUserId(userInfo.getId());
			}
			
			SubjectUserRecordBO subjectUserRecordBO = SubjectUserRecordDTOConverter.INSTANCE.convertDtoToBo(subjectUserRecordDTO);
			
			List<SubjectUserRecordBO> subjectUserRecordBOList =
					subjectUserRecordDomainService.getAllSubjectUserRecord(subjectUserRecordBO);
			
			return Result.ok(SubjectUserRecordDTOConverter.INSTANCE.convertBoListToDto(subjectUserRecordBOList));
		} catch (Exception e) {
			log.error("SubjectUserRecordController.getAllSubjectUserRecord.error:{}", e.getMessage(), e);
			return Result.fail("获取用户所有的做题记录失败");
		}
	}
	
}